import datetime
import random
from function_tool import time_format_transform
import pymysql

import clickhouse_connect

connect_info = {
    'port': 8123,
    'host': '192.168.104.243',
    'username': 'default',
    'password': 'bwda2025',
    'database': 'lap'
}
mysql_connect_info = {
    'port': 28002,
    'host': '192.168.2.210',
    'user': 'root',
    'password': 'cI5v4N2Q%a',
    'database': 'lap'
}
mysql_connection = pymysql.connect(**mysql_connect_info)
mysql_cursor = mysql_connection.cursor()
sql = """
        select distinct bag.dept_id, 
        case when bag.group_type = 2 then bdo.org_name
             when bag.group_type = 1 then concat(bdo1.org_name, '-', bdo.org_name)
             when bag.group_type = 0 then concat(bdo2.org_name, '-', bdo1.org_name, '-', bdo.org_name)
             end as description, bag.dept_parent_id
        from bpm_audit_group bag
        left join basic_data_org bdo on bdo.org_id = bag.dept_id
        left join system_users su on su.id = bag.user_id
        left join basic_data_org bdo1 on bdo1.org_id = bag.dept_parent_id
        left join bpm_audit_group bag1 on bag.dept_parent_id = bag1.dept_id
        left join basic_data_org bdo2 on bdo2.org_id = bag1.dept_parent_id
        where bag.deleted = 0 and bag.dept_id not in (1)
"""
mysql_cursor.execute(sql)
result = mysql_cursor.fetchall()
mysql_cursor.close()
mysql_connection.close()
client = clickhouse_connect.get_client(**connect_info)
column_list = ['bwd_create_time', 'kz_op_work_time_flag', 'op_time', 'zy_slave_account_id', 'kz_slave_account_type',
               'zy_slave_account_name', 'id',
                                        'zy_master_account_id', 'zy_master_parent_org_id', 'zy_master_account_name',
               'zy_master_org_name_path',
               'zy_master_org_id', 'zy_service_ip',
               'zy_client_ip', 'kz_op_result', 'zy_master_account_type', 'kz_op_type_code', 'zy_master_valid_state']
table_name = 'dws_login_log_local'
insert_data = []
kz_op_work_time_flag_list = ['0', '1']
kz_slave_account_type_list = ['0', '1', '2', '3', '4', '5', '6', '99']
zy_master_account_type_list = ['00', '01']
op_result_list = ['0', '1']
zy_master_account_state_list = ['删除', '注销', '加锁', '正常']
length = len(result)
for i in range(8000):
    index = random.randint(0, length - 1)
    id = str(random.randint(10 ** 14, 10 ** 16))
    bwd_create_time = time_format_transform.TimeFormatTransform().get_random_date('2025-08-20', '2025-09-01')
    # bwd_create_time = datetime.datetime.strptime('2025-08-06 12:00:00', '%Y-%m-%d %H:%M:%S')
    kz_op_work_time_flag = random.choice(kz_op_work_time_flag_list)
    op_time = bwd_create_time
    zy_slave_account_id = 'luffy'
    kz_slave_account_type = random.choice(kz_slave_account_type_list)
    zy_slave_account_name = '路飞'
    zy_master_account_id = 'monkey.D.luffy' + str(i).zfill(5)
    zy_master_parent_org_id = str(result[index][2])
    zy_master_account_name = '蒙奇.D.路飞'
    zy_master_org_name_path = result[index][1]
    zy_master_org_id = str(result[index][0])
    service_ip = '82.221.10.49'
    client_ip = '82.221.10.50'
    op_result = random.choice(op_result_list)
    zy_master_account_type = random.choice(zy_master_account_type_list)
    op_type = ''
    zy_master_account_state = random.choice(zy_master_account_state_list)
    insert_data.append([bwd_create_time, kz_op_work_time_flag, op_time, zy_slave_account_id, kz_slave_account_type,
                        zy_slave_account_name, id, zy_master_account_id, zy_master_parent_org_id, zy_master_account_name,
                        zy_master_org_name_path, zy_master_org_id, service_ip, client_ip,
                        op_result, zy_master_account_type, op_type, zy_master_account_state])
client.insert(table_name, insert_data, column_list)
client.close()
